package com.cxh.controller.carAiUi;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.alibaba.fastjson.JSON;
import com.google.gson.GsonBuilder;
import com.cxh.base.BaseController;
import com.cxh.entity.CarActivity;
import com.cxh.entity.CarCategory;
import com.cxh.entity.CarCompetitive;
import com.cxh.entity.CarLoan;
import com.cxh.entity.CarLoanMonthNum;
import com.cxh.entity.CarLoanMonthNumPay;
import com.cxh.entity.CarLoanPlan;
import com.cxh.entity.CarModel;
import com.cxh.entity.DataEntity;
import com.cxh.util.CommonUtility;
import com.cxh.util.LogUtil;

@Controller
@RequestMapping("/controller/AiuiAPI")
public class AiuiAPIController extends BaseController{
	
	@RequestMapping("/carCategory")
	public void carCategory(HttpServletRequest request, HttpServletResponse response){
		StringBuffer requestURL = request.getRequestURL();
		String queryString = request.getQueryString(); 
		try {
			LogUtil.info(requestURL+URLDecoder.decode(queryString,"utf-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		@SuppressWarnings("unchecked")
		Map<String, Object> paramsMap = (Map<String, Object>) request.getAttribute("params");
		Object carCategoryIdObject = paramsMap.get("carCategoryId");
		//介绍方式
		Object way = paramsMap.get("way");
		LogUtil.info("进入carCategory方法=====参数："+paramsMap);
		//请求参数
		HashMap<String, Object> requestParam = new HashMap<String,Object>();
		requestParam.put("carCategoryId", carCategoryIdObject);
		String categoryName = carCategoryService.getCategoryName(carCategoryIdObject.toString());
		requestParam.put("way", way);
		requestParam.put("carCategoryName", categoryName);
		requestParam.put("competitiveCategoryName","");
		requestParam.put("beginPrice", "");
		requestParam.put("endPrice", "");
		requestParam.put("gear", "");
		requestParam.put("level", "");
		requestParam.put("output", "");
		requestParam.put("TOrL", "");
		requestParam.put("carmodelid", "");
		requestParam.put("year", "");
		
		if(CommonUtility.isEmpty(way)){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("scene", 0);
			map.put("skill",0);
			map.put("msgType",0);
			map.put("flag", 0);
			map.put("msg", "返回成功");
			map.put("speak", "为您找到该车的外观、内饰、动力、科技、安全方面的介绍和厂家视频介绍,您想选择哪一个呢？");
			map.put("url","");
			map.put("set", new Object());
			map.put("dataStructure", request.getAttribute("page"));
			map.put("skillList",new Object[]{});
			map.put("requestParam",requestParam);
			GsonBuilder gb= new GsonBuilder();
			gb.disableHtmlEscaping();
			String json = gb.create().toJson(map);
			System.out.println(json);
			try {
				super.ajaxOutput(response, JSON.toJSONString(map));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			String carCategoryId = carCategoryIdObject.toString();
			//匹配汽车信息,并介绍
			getCarIntroduce(request,response,carCategoryId,way.toString(),requestParam);
		}
	}
	
	/**
	 * @param way 
	 * @param requestParam 
	 * @类功能说明：匹配汽车信息,并介绍
	 * @作者：Chenxh
	 * @创建时间：2018年10月24日上午8:56:03
	 */
	private void getCarIntroduce(HttpServletRequest request, HttpServletResponse response,String carCategoryId, String way, HashMap<String, Object> requestParam) {
		StringBuffer requestURL = request.getRequestURL();
		String queryString = request.getQueryString(); 
		try {
			LogUtil.info(requestURL+URLDecoder.decode(queryString,"utf-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		LogUtil.info("getCarIntroduce方法================");
		Example example = new Example(CarCategory.class);
		Criteria createCriteria = example.createCriteria();
		createCriteria.andEqualTo("carcategoryid",carCategoryId);
		String code = request.getAttribute("code").toString();
		
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		try {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("scene", 0);
			map.put("skill",0);
			map.put("msgType",0);
			map.put("flag", 0);
			map.put("msg", "返回成功");
			map.put("set", new Object());
			map.put("dataStructure", request.getAttribute("page"));
			map.put("skillList",new Object[]{});
			map.put("requestParam",requestParam);
			//外观、内饰、动力、科技、安全
			if("外观".equals(way)){
				map.put("speak", "为您找到该车的外观信息请阅览");
				map.put("dataStructure", "CarInfo_Introduce_Facade");
			}
			if("内饰".equals(way)){
				map.put("speak", "为您找到该车的内饰信息请阅览");
				map.put("dataStructure", "CarInfo_Introduce_Decorate");
			}
			if("动力".equals(way)){
				map.put("speak", "为您找到该车的动力信息请阅览");
				map.put("dataStructure", "CarInfo_Introduce_Powe");
			}
			if("科技".equals(way)){
				map.put("speak", "为您找到该车的科技信息请阅览");
				map.put("dataStructure", "CarInfo_Introduce_Sciencetechnology");
			}
			if("安全".equals(way)){
				map.put("speak", "为您找到该车的安全信息请阅览");
				map.put("dataStructure", "CarInfo_Introduce_Security");
			}
			if("视频".equals(way)){
				map.put("speak", "为您找到该车的视频信息请阅览");
				map.put("dataStructure", request.getAttribute("page"));
			}
			//map.put("url",basePath+"controller/CarInterface/carIntroduce?carCategoryId="+carCategoryId+"&way="+way+"&code="+code);
			map.put("url","");
			GsonBuilder gb= new GsonBuilder();
			gb.disableHtmlEscaping();
			String json = gb.create().toJson(map);
			System.out.println(json);
			try {
				super.ajaxOutput(response, JSON.toJSONString(map));
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/carBudget")
	public void carBudget(HttpServletRequest request, HttpServletResponse response){
		StringBuffer requestURL = request.getRequestURL();
		String queryString = request.getQueryString(); 
		try {
			LogUtil.info(requestURL+URLDecoder.decode(queryString,"utf-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
		@SuppressWarnings("unchecked")
		Map<String, Object> paramsMap = (Map<String, Object>) request.getAttribute("params");
		
		Double beginPrice = Double.parseDouble(paramsMap.get("beginPrice")==null?"0":paramsMap.get("beginPrice")+"");
		Double endPrice =  Double.parseDouble(paramsMap.get("endPrice")==null?"0":paramsMap.get("endPrice")+"");
		//手动挡、自动挡
		Object gear =  paramsMap.get("gear");
		//车类型级别
		Object level =  paramsMap.get("level");
		//汽车排量
		Object output =  paramsMap.get("output");
		//进气形式
		Object TOrL =  paramsMap.get("TOrL");
		//请求报文参数
		HashMap<String, Object> requstParam = new HashMap<String,Object>();
		requstParam.put("beginPrice", beginPrice);
		requstParam.put("endPrice", endPrice);
		if(!CommonUtility.isEmpty(gear)){
			if("手动".equals(gear)){
				requstParam.put("gear", "自动");
			}else{
				requstParam.put("gear", "手动");
			}
		}else{
			requstParam.put("gear", "");
		}
		requstParam.put("level", level==null?"":level);
		requstParam.put("output", output==null?"":output);
		requstParam.put("TOrL", TOrL==null?"":TOrL);
		requstParam.put("way", "");
		requstParam.put("carCategoryName", "");
		requstParam.put("carCategoryId", "");
		requstParam.put("competitiveCategoryName","");
		requstParam.put("carmodelid", "");
		requstParam.put("year", "");
		
		CarModel carModel = new CarModel();
		List<CarModel> carModelList = new ArrayList<CarModel>();
		
		carModel.setBeginPrice((new Double(beginPrice)).intValue());
		carModel.setEndPrice((new Double(endPrice)).intValue());
		carModel.setCarclassifyid("1");
		if(!CommonUtility.isEmpty(gear)){
			carModel.setGearbox(gear.toString());
		}
		if(!CommonUtility.isEmpty(level)){
			carModel.setLevel(level.toString());
		}
		if(!CommonUtility.isEmpty(output)){
			carModel.setOutput(output.toString());
			long longValue = new BigDecimal(output.toString()).setScale( 0, BigDecimal.ROUND_DOWN ).longValue();// 向下取整
			carModel.setOutputDown(longValue - 100);
			carModel.setOutputUp(longValue + 100);
		}
		if(!CommonUtility.isEmpty(TOrL)){
			carModel.setTOrL(TOrL.toString());
		}
		
		carModelList = carModelService.getPriceData(carModel,request);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("scene", 0);
		map.put("skill",0);
		map.put("msgType",0);
		map.put("flag", 0);
		map.put("msg", "返回成功");
		map.put("dataStructure",request.getAttribute("page"));
		if(carModelList.size()!=0){
			if(gear == null){
				map.put("speak", "为您找到该系列类型的车如图，请问您要选择手动挡还是自动挡呢？");
				//map.put("url",basePath+"controller/CarInterface/carQuestionSurvey?beginPrice="+beginPrice.intValue()+"&endPrice="+endPrice.intValue());
				map.put("url","");
				map.put("set", new Object());
				map.put("skillList",new Object[]{});
				map.put("requestParam",requstParam);
				//map.put("carModelList",carModelList);

				GsonBuilder gb= new GsonBuilder();
				gb.disableHtmlEscaping();
				String json = gb.create().toJson(map);
				System.out.println(json);
				try {
					super.ajaxOutput(response, JSON.toJSONString(map));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}else{
				if(level == null){
					//查询车型分组 
					List<String> modelList = carModelService.getModel(carModel);
					String carModelTypeNameStr = "";
					for (String carModelTypeName : modelList) {
						carModelTypeNameStr += "还是"+carModelTypeName;
					}
					if(carModelTypeNameStr.length()>0){
						carModelTypeNameStr = carModelTypeNameStr.substring(2);
					}
					map.put("speak", "请问您要选择"+carModelTypeNameStr+"呢？");
					//map.put("url",basePath+"controller/CarInterface/carQuestionSurvey?beginPrice="+beginPrice.intValue()+"&endPrice="+endPrice.intValue()+"&gear="+gear);
					map.put("url","");
					map.put("set", new Object());
					map.put("skillList",new Object[]{});
					map.put("requestParam",requstParam);
					//map.put("carModelList",carModelList);
					GsonBuilder gb= new GsonBuilder();
					gb.disableHtmlEscaping();
					String json = gb.create().toJson(map);
					System.out.println(json);
					try {
						super.ajaxOutput(response, JSON.toJSONString(map));
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				if(output == null && TOrL == null){
					String info = "";
					//去掉重复的
					HashSet<String> hashSet = new HashSet<String>();
					
					for (CarModel carModel2 : carModelList) {
						String output2 = carModel2.getOutput();
						String tOrL2 = carModel2.getTOrL();
						if("自然吸气".equals(tOrL2)){
							tOrL2 = "L";
						}else{
							tOrL2 = "T";
						}
						//四舍五入保留一位小数
						BigDecimal setScale = new BigDecimal(output2).setScale(1, BigDecimal.ROUND_HALF_UP);
						BigDecimal b = new BigDecimal(1000);
						BigDecimal divide = setScale.divide(b,1, BigDecimal.ROUND_HALF_UP);
						hashSet.add(divide+tOrL2);
					}
					for (String outputTOrL : hashSet) {
						info += "，" + outputTOrL;
					}
					info = info.substring(1);
					map.put("speak", "为您找到"+info+"请问您要选择什么样的排量发动机呢？");
					//map.put("url",basePath+"controller/CarInterface/carQuestionSurvey?beginPrice="+beginPrice.intValue()+"&endPrice="+endPrice.intValue()+"&gear="+gear+"&level="+level);
					map.put("url","");
					map.put("set", new Object());
					map.put("skillList",new Object[]{});
					map.put("requestParam",requstParam);
					//map.put("carModelList",carModelList);
					GsonBuilder gb= new GsonBuilder();
					gb.disableHtmlEscaping();
					String json = gb.create().toJson(map);
					System.out.println(json);
					try {
						super.ajaxOutput(response, JSON.toJSONString(map));
					} catch (Exception e) {
						e.printStackTrace();
					}
				}else{
					map.put("speak", "请问您想了解下面哪一类别的车呢？");
					map.put("requestParam",requstParam);
					//map.put("carModelList",carModelList);
				}
			}
			/*map.put("url",basePath+"controller/CarInterface/carQuestionSurvey?beginPrice="+(new Double(beginPrice)).intValue()+"&endPrice="+(new Double(endPrice)).intValue()+
					"&gear="+gear+"&level="+level+"&output="+output+"&TOrL="+TOrL);*/
			map.put("url","");
		}else{
			if(gear == null){
				map.put("speak", "未能给您找到合适价格区间的车");
				map.put("requestParam",requstParam);
				map.put("url","");
			}else{
				map.put("flag",1);
			}
		}
		map.put("set", new Object());
		map.put("skillList",new Object[]{});
		GsonBuilder gb= new GsonBuilder();
		gb.disableHtmlEscaping();
		String json = gb.create().toJson(map);
		System.out.println(json);
		try {
			super.ajaxOutput(response, JSON.toJSONString(map));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @类功能说明：方案页面
	 * @作者：Chenxh
	 * @创建时间：2018年10月18日下午2:01:21
	 */
	@RequestMapping("/getLoanPlanPage")
	public String getLoanPlanPage(HttpServletRequest request, HttpServletResponse response){
		
		
		StringBuffer requestURL = request.getRequestURL();
		String queryString = request.getQueryString(); 
		try {
			LogUtil.info(requestURL+URLDecoder.decode(queryString,"utf-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//map.put("url",basePath+"controller/AiuiAPI/getLoanPlanPage?monthnum="+monthnum+"&carModelId="+carCategoryId+"&carLoanMoney="+money);

		//期限
		String monthnum = request.getParameter("monthnum");
		//汽车系列
		String carCategoryId = request.getParameter("carCategoryId");
		//贷款金额
		String carLoanMoney = request.getParameter("carLoanMoney");
		//月供
		String monthnumMoney = request.getParameter("monthnumMoney");
		
		//查询汽车信息
		Example example = new Example(CarCategory.class);
		Criteria createCriteria = example.createCriteria();
		createCriteria.andEqualTo("carcategoryid",carCategoryId);
		List<CarCategory> carCategoryList = carCategoryService.selectByExample(example);
		
		request.setAttribute("carCategory", carCategoryList.get(0).getCarcategoryname());
		request.setAttribute("monthnum", monthnum);
		request.setAttribute("carLoanMoney", carLoanMoney);
		request.setAttribute("monthnumMoney", monthnumMoney);
		return "car/loanPlanPage1";
	}
/*	*//**
	 * @类功能说明：方案页面
	 * @作者：Chenxh
	 * @创建时间：2018年10月18日下午2:01:21
	 *//*
	@RequestMapping("/getLoanPlanPage")
	public String getLoanPlanPage(HttpServletRequest request, HttpServletResponse response){
		//可行的计划方案id串
		String planIdInfo = request.getParameter("planIdInfo");
		//汽车款型id
		String carModelId = request.getParameter("carModelId");
		//贷款金额
		String carLoanMoney = request.getParameter("carLoanMoney");
		CarModel carModel = new CarModel();
		carModel.setCarmodelid(carModelId);
		//汽车款型id查询汽车详情
		List<CarModel> carModelList = carModelService.getPriceData(carModel);
		carModel = carModelList.get(0);
		
		//汽车贷款可行方案集合
		ArrayList<CarLoanPlan> carLoanPlanList = new ArrayList<CarLoanPlan>();
		
		String[] planIds = planIdInfo.split(",");
		for (String planId : planIds) {
			CarLoanPlan carLoanPlan = carLoanPlanService.selectByPrimaryKey(planId);
			carLoanPlanList.add(carLoanPlan);
		}
		
		request.setAttribute("carLoanPlanList", carLoanPlanList);
		request.setAttribute("carModel", carModel);
		request.setAttribute("carLoanMoney", carLoanMoney);
		return "car/loanPlanPage";
	}
*/	

	private CarModel getCarMoney(String carCategoryId,String year, String carModelName,HttpServletRequest request) {
		CarModel carModel = new CarModel();
		carModel.setCarcategoryid(carCategoryId);
		carModel.setCaryear(year);
		carModel.setOtherName(carModelName);
		carModel.setCarclassifyid("1");
		List<CarModel> carModelList = carModelService.getPriceData(carModel,request);
		
		return carModelList.get(0);
	}
	
	
	/**
	 * @类功能说明：汽车配置详情
	 * @作者：Chenxh
	 * @创建时间：2018年10月25日上午11:15:20
	 */
	@RequestMapping("/carDetail")
	public void carDetailCatagory(HttpServletRequest request, HttpServletResponse response){
		StringBuffer requestURL = request.getRequestURL();
		String queryString = request.getQueryString(); 
		try {
			LogUtil.info(requestURL+URLDecoder.decode(queryString,"utf-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		@SuppressWarnings("unchecked")
		Map<String, Object> paramsMap = (Map<String, Object>) request.getAttribute("params");
		//车类别
		Object carCategory =  paramsMap.get("carCategory");
		//车年份
		Object year =  paramsMap.get("year");
		//车型别名
		Object otherName1 =  paramsMap.get("model");
		//页面
		 Object page = request.getAttribute("page");
		 
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
		
		CarModel carModel = new CarModel();
		if(!CommonUtility.isEmpty(carCategory)){
			carModel.setCarcategoryid(carCategory.toString());
		}
		if(!CommonUtility.isEmpty(year)){
			carModel.setCaryear(year.toString());
		}
		if(!CommonUtility.isEmpty(otherName1)){
			/*carModel.setCarCategoryName(model.toString());*/
			carModel.setOtherName(otherName1.toString());
		}
		carModel.setCarclassifyid("1");
		List<CarModel> carModelList = carModelService.getPriceData(carModel,request);
		//map.put("carModelList",carModelList);
		//按照名称和年份分类，看下这类型的车有几个年份的，有多个年份的话后面就问，没有的话就不问年份了
		List<CarModel> carModelByYearList = carModelService.getPriceDataByYear(carModel,request);
		//报文的map
		HashMap<String, Object> map = new HashMap<String,Object>(); 	
		//requstParam的map
		HashMap<String, Object> requstParamMap = new HashMap<String, Object>();
		if(carModelList.size()!=0){
			String otherNames = ""; 
			for (CarModel carModel2 : carModelList) {
				//获得汽车车型的别名
				String otherName = carModel2.getOtherName();
				otherNames += ","+otherName;
			}
			otherNames = otherNames.substring(1, otherNames.length());
			if(!CommonUtility.isEmpty(carCategory)){	
				requstParamMap.put("carCategoryId", carCategory);
				//查询车系名称
				String categoryName = carCategoryService.getCategoryName(carCategory.toString());
				requstParamMap.put("carCategoryName", categoryName);
				requstParamMap.put("beginPrice", "");
				requstParamMap.put("endPrice", "");
				requstParamMap.put("gear", "");
				requstParamMap.put("level", "");
				requstParamMap.put("output", "");
				requstParamMap.put("TOrL", "");
				requstParamMap.put("way", "");
				requstParamMap.put("competitiveCategoryName","");
				requstParamMap.put("year", "");
				requstParamMap.put("carmodelid", "");
				requstParamMap.put("year", year);
				
				if(CommonUtility.isEmpty(year) && CommonUtility.isEmpty(otherName1) && carModelByYearList.size()>1){
					map.put("speak", "为您找到该系列类型的车如图,请问您要了解哪一年份的车型呢？");
					map.put("url","");
					//map.put("url",basePath+"controller/CarInterface/carDetail?carCategoryId="+carCategory);
					map.put("dataStructure",request.getAttribute("page"));
					map.put("requestParam", requstParamMap);
				}else if(CommonUtility.isEmpty(otherName1)){
					map.put("speak", "已为您找到"+otherNames+"车如图，请问您想了解哪一车型的呢？");
					map.put("url","");
					//map.put("url",basePath+"controller/CarInterface/carDetail?carCategoryId="+carCategory+"&year="+year);
					requstParamMap.put("year", year);
					map.put("dataStructure",request.getAttribute("page"));
					map.put("requestParam", requstParamMap);
				}else{
					if(carModelList.size() == 1){
						carModel = carModelList.get(0);
						requstParamMap.put("carmodelid", carModel.getCarmodelid());
						map.put("url","");
						//map.put("url",basePath+"controller/CarInterface/getCarModelDetail?carmodelid="+carModel.getCarmodelid());
						map.put("dataStructure",request.getAttribute("page"));
						map.put("requestParam", requstParamMap);
					}else{
						map.put("url", "");
					}
					map.put("speak", "为您找到该车配置如图");
				}
			}
		}else{
			map.put("speak", "很抱歉，未能给您找到该类型的汽车信息");
			map.put("url","");
			requstParamMap.put("carCategoryId", carCategory);
			//查询车系名称
			String categoryName = carCategoryService.getCategoryName(carCategory.toString());
			requstParamMap.put("carCategoryName", categoryName);
			requstParamMap.put("beginPrice", "");
			requstParamMap.put("competitiveCategoryName","");
			requstParamMap.put("endPrice", "");
			requstParamMap.put("gear", "");
			requstParamMap.put("level", "");
			requstParamMap.put("output", "");
			requstParamMap.put("TOrL", "");
			requstParamMap.put("way", "");
			requstParamMap.put("carmodelid", "");
			requstParamMap.put("year", year);
			map.put("requestParam", requstParamMap);
		}
		map.put("scene", 0);
		map.put("skill",0);
		map.put("msgType",0);
		map.put("flag", 0);
		map.put("msg", "返回成功");
		map.put("skillList",new Object[]{});
		map.put("set", new Object());
		map.put("skillList",new Object[]{});
		map.put("dataStructure",page);
		GsonBuilder gb= new GsonBuilder();
		gb.disableHtmlEscaping();
		String json = gb.create().toJson(map);
		System.out.println(json);
		try {
			super.ajaxOutput(response, JSON.toJSONString(map));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * @类功能说明：查询汽车配置详情
	 * @作者：Chenxh
	 * @创建时间：2018年10月25日下午1:56:31
	 */
	@RequestMapping("/getCarModelDetail")
	public String getCarModelDetail(HttpServletRequest request, HttpServletResponse response){
		
		StringBuffer requestURL = request.getRequestURL();
		String queryString = request.getQueryString(); 
		try {
			LogUtil.info(requestURL+URLDecoder.decode(queryString,"utf-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		String carCategoryId =  request.getParameter("carCategoryId");
		String otherName =  request.getParameter("model");
		String year =  request.getParameter("year");
		
		//查询该车
		CarModel carModel = new CarModel();
		if(!CommonUtility.isEmpty(carCategoryId)){
			carModel.setCarcategoryid(carCategoryId);
		}
		if(!CommonUtility.isEmpty(otherName)){
			//carModel.setCarmodelname(model);
			carModel.setOtherName(otherName);
		}
		if(!CommonUtility.isEmpty(year)){
			carModel.setCaryear(year);
		}
		carModel.setCarclassifyid("1");
		List<CarModel> CarModelList = carModelService.getPriceData(carModel,request);
		if(CarModelList.size() == 1){
			carModel = CarModelList.get(0);
			request.setAttribute("carModel", carModel);
		}
		//汽车类型的id
		String carmodelid = carModel.getCarmodelid();
		//查询各个表中的汽车详情的信息
		getEveryCarDetail(request,carmodelid);
		return "car/carDetailPage";
	}

	/**
	 * @类功能说明：查询各个表中的汽车详情的信息，把信息放到request中
	 * @作者：Chenxh
	 * @创建时间：2018年10月25日下午2:02:12
	 */
	private void getEveryCarDetail(HttpServletRequest request, String carmodelid) {
		//获取所有的汽车详情信息
		Map<String, Object> carDetailInfo = carDetailBaseInfoService.getInfoByCarmodelId(carmodelid);
		request.setAttribute("carDetailInfo", carDetailInfo);
	}
	
	/**
	 * @类功能说明：汽车竞品
	 * @作者：Chenxh
	 * @创建时间：2018年10月26日上午10:49:43
	 */
	@RequestMapping("/carCompetitive")
	public void carCompetitive(HttpServletRequest request,HttpServletResponse response){
		@SuppressWarnings("unchecked")
		Map<String, String> paramsMap = (Map<String, String>) request.getAttribute("params");
		//获取比较车辆的名称
		String carCategory = paramsMap.get("carCategory");

		String competitiveCategoryName = paramsMap.get("competitiveCategoryName");
		
		//请求参数集合
		HashMap<String, Object> requstParamMap = new HashMap<String,Object>();
		requstParamMap.put("carCategoryId", carCategory);
		String categoryName = carCategoryService.getCategoryName(carCategory);
		requstParamMap.put("carCategoryName", categoryName);
		
		requstParamMap.put("beginPrice", "");
		requstParamMap.put("endPrice", "");
		requstParamMap.put("gear", "");
		requstParamMap.put("level", "");
		requstParamMap.put("output","");
		requstParamMap.put("TOrL","");
		requstParamMap.put("way", "");
		requstParamMap.put("competitiveCategoryName","");
		requstParamMap.put("carmodelid", "");
		requstParamMap.put("year", "");
		
		Example example2 = new Example(CarCategory.class);
		Criteria createCriteria2 = example2.createCriteria();
		createCriteria2.andEqualTo("carcategoryid",carCategory);
		List<CarCategory> carCategoryList = carCategoryService.selectByExample(example2);
		
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
		
		if(!CommonUtility.isEmpty(competitiveCategoryName)){
			requstParamMap.put("competitiveCategoryName", competitiveCategoryName);

			//查询数据库
			Example example = new Example(CarCompetitive.class);
			Criteria createCriteria = example.createCriteria();
			createCriteria.andEqualTo("categoryname",carCategoryList.get(0).getCarcategoryname());
			createCriteria.andEqualTo("competitivecategoryname",competitiveCategoryName);
			List<CarCompetitive> carCompetitiveList = carCompetitiveService.selectByExample(example);
			
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("dataStructure", request.getAttribute("page"));
			map.put("url","");
			//map.put("url",basePath+"controller/CarInterface/carCompetitive?carCategoryName="+carCompetitiveList.get(0).getCategoryname()+"&competitiveCategoryName="+competitiveCategoryName);
			map.put("scene", 0);
			map.put("skill",0);
			map.put("msgType",0);
			map.put("flag", 0);
			map.put("msg", "返回成功");
			map.put("speak", "");
			map.put("set", new Object());
			map.put("skillList",new Object[]{});
			map.put("requestParam",requstParamMap);
			GsonBuilder gb= new GsonBuilder();
			gb.disableHtmlEscaping();
			try {
				super.ajaxOutput(response, JSON.toJSONString(map));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			//查询数据库
			/*Example example = new Example(CarCompetitive.class);
			Criteria createCriteria = example.createCriteria();
			createCriteria.andEqualTo("categoryname",carCategoryList.get(0).getCarcategoryname());
			List<CarCompetitive> carCompetitiveList = carCompetitiveService.selectByExample(example);*/
			
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("dataStructure", request.getAttribute("page"));
			map.put("scene", 0);
			map.put("skill",0);
			map.put("msgType",0);
			map.put("flag", 0);
			map.put("msg", "返回成功");
			map.put("speak", "为您找到以下竞品，请问你要选择哪一个进行比较呢？");
			//map.put("url",basePath+"controller/CarInterface/getCarCompetitive?carCategoryName="+carCompetitiveList.get(0).getCategoryname());
			map.put("url","");

			map.put("set", new Object());
			map.put("skillList",new Object[]{});
			map.put("requestParam",requstParamMap);
			GsonBuilder gb= new GsonBuilder();
			gb.disableHtmlEscaping();
			try {
				super.ajaxOutput(response, JSON.toJSONString(map));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @类功能说明：最低贷款
	 * @作者：Chenxh
	 * @创建时间：2018年10月27日上午10:33:57
	 */
	@RequestMapping("/carLowPrice")
	public void carLowPrice(HttpServletRequest request,HttpServletResponse response){
		StringBuffer requestURL = request.getRequestURL();
		String queryString = request.getQueryString(); 
		try {
			LogUtil.info(requestURL+URLDecoder.decode(queryString,"utf-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		@SuppressWarnings("unchecked")
		Map<String, String> paramsMap = (Map<String, String>) request.getAttribute("params");
		
		//系列id
		String carCategoryId = paramsMap.get("carCategoryId");
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("scene", 0);
		map.put("skill",0);
		map.put("msgType",0);
		map.put("flag", 0);
		map.put("msg", "返回成功");
		map.put("dataStructure", request.getAttribute("page"));
		
		//请求参数集合
		HashMap<String, Object> requstParamMap = new HashMap<String,Object>();
		requstParamMap.put("carCategoryId", carCategoryId);
		String categoryName = carCategoryService.getCategoryName(carCategoryId);
		requstParamMap.put("carCategoryName", categoryName);
		
		requstParamMap.put("beginPrice", "");
		requstParamMap.put("endPrice",  "");
		requstParamMap.put("gear",  "");
		requstParamMap.put("level",  "");
		requstParamMap.put("output",  "");
		requstParamMap.put("TOrL",  "");
		requstParamMap.put("way", "");
		requstParamMap.put("competitiveCategoryName","");
		requstParamMap.put("carmodelid", "");
		requstParamMap.put("year", "");
		
		//根据车系查询贷款方案
		Example example2 = new Example(CarLoan.class);
		Criteria createCriteria2 = example2.createCriteria();
		createCriteria2.andEqualTo("carcategoryid",carCategoryId);
		List<CarLoan> CarLoanList = carLoanService.selectByExample(example2);
		
		//查询车系名称
		Example example = new Example(CarCategory.class);
		Criteria createCriteria = example.createCriteria();
		createCriteria.andEqualTo("carcategoryid",carCategoryId);
		List<CarCategory> carCategoryList = carCategoryService.selectByExample(example);
		
		map.put("carCategoryId", carCategoryId);
		
		if(CarLoanList.size()!=0){
			//贷款金额
			BigDecimal money = new BigDecimal(CarLoanList.get(0).getMoney());
			//期限
			BigDecimal monthnum = new BigDecimal(CarLoanList.get(0).getMonthnum());
			//月供
			BigDecimal monthnumMoney = money.divide(monthnum,2,BigDecimal.ROUND_HALF_UP);

			map.put("url","");
			map.put("speak", "已为您找到"+carCategoryList.get(0).getCarcategoryname()+"优惠方案，贷"+money+"元，"+monthnum+"个月零利息，月供"+monthnumMoney+"元就能取走车");
		}else{
			map.put("speak", "未能给您找到优惠方案");
			map.put("url","");
		}	
		map.put("set", new Object());
		map.put("skillList",new Object[]{});
		map.put("requestParam",requstParamMap);
		map.put("dataStructure","CarInfor_Promotion");
		
		GsonBuilder gb= new GsonBuilder();
		gb.disableHtmlEscaping();
		String json = gb.create().toJson(map);
		System.out.println(json);
		try {
			super.ajaxOutput(response, JSON.toJSONString(map));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @类功能说明：汽车活动介绍
	 * @作者：Chenxh
	 * @创建时间：2018年10月29日上午9:36:37
	 */
	@RequestMapping("/carActivity")
	public void carActivity(HttpServletRequest request,HttpServletResponse response){
		StringBuffer requestURL = request.getRequestURL();
		String queryString = request.getQueryString(); 
		try {
			LogUtil.info(requestURL+URLDecoder.decode(queryString,"utf-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		@SuppressWarnings("unchecked")
		Map<String, String> paramsMap = (Map<String, String>) request.getAttribute("params");
		//系列id
		String carCategoryId = paramsMap.get("carCategoryId");
		Example example = new Example(CarActivity.class);
		Criteria createCriteria = example.createCriteria();
		createCriteria.andEqualTo("carcategoryid",carCategoryId);
		//查询优惠表
		List<CarActivity> carActivitylist = carActivityService.selectByExample(example);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("scene", 0);
		map.put("skill",0);
		map.put("msgType",0);
		map.put("flag", 0);
		map.put("msg", "返回成功");
		map.put("url","");
		if(carActivitylist.size() != 0){
			map.put("speak", carActivitylist.get(0).getActivityintroduce());
		}else{
			map.put("speak", "未能给您找到该系列车的优惠");
		}
		map.put("set", new Object());
		map.put("skillList",new Object[]{});
		GsonBuilder gb= new GsonBuilder();
		gb.disableHtmlEscaping();
		String json = gb.create().toJson(map);
		System.out.println(json);
		try {
			super.ajaxOutput(response, JSON.toJSONString(map));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @类功能说明：优惠活动
	 * @作者：Chenxh
	 * @创建时间：2018年10月29日上午9:36:37
	 */
	@RequestMapping("/preferentialActivities")
	public void preferentialActivities(HttpServletRequest request,HttpServletResponse response){
		StringBuffer requestURL = request.getRequestURL();
		String queryString = request.getQueryString(); 
		try {
			LogUtil.info(requestURL+URLDecoder.decode(queryString,"utf-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		//机器人编码
		String robotCode = request.getParameter("code");
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
				
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("scene", 0);
		map.put("skill",0);
		map.put("msgType",0);
		map.put("flag", 0);
		map.put("msg", "返回成功");
		map.put("url","");
		//map.put("url",basePath+"controller/CarInterface/getcarDiscount?robotCode="+robotCode);
		map.put("speak", "为您找到优惠活动如下");
		map.put("set", new Object());
		map.put("skillList",new Object[]{});
		GsonBuilder gb= new GsonBuilder();
		gb.disableHtmlEscaping();
		String json = gb.create().toJson(map);
		System.out.println(json);
		try {
			super.ajaxOutput(response, JSON.toJSONString(map));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
